package com.lst.servlet;

import java.io.PrintWriter;
import java.sql.Time;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.druid.util.StringUtils;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.lst.base.entity.BaseRequest;
import com.lst.base.entity.BaseResponse;
import com.lst.common.CommCode;
import com.lst.entity.request.FreeRunReqPara;
import com.lst.model.FreeRun;
import com.lst.service.FreeRunService;
import com.lst.utils.DateUtil;
import com.lst.utils.RequestUtils;


/**
 * @ClassName: FreeRunServlet
 * @Description: 自由跑接口
 * 
 */
public class FreeRunServlet extends BaseServlet {

	/**
	 * @Fields serialVersionUID
	 */
	private static final long serialVersionUID = 1L;

	private FreeRunService freeRunService = ctx.getBean(FreeRunService.class);
	
	@Override
	public void doGet(HttpServletRequest request, HttpServletResponse response) {
		doPost(request, response);
	}

	@Override
	public void doPost(HttpServletRequest request, HttpServletResponse response) {
		Date startDate = new Date();
		BaseResponse resPara = new BaseResponse();

		try {
			FreeRunReqPara reqPara = getReqPara(request);
		
		if(isCheckField(reqPara,resPara)){
			Integer userid = Integer.valueOf(reqPara.getUserid());
			Double  roadLength = Double.valueOf(reqPara.getRoadLength());
			Double  speed = Double.valueOf(reqPara.getSpeed());
			Integer feelStatus = Integer.valueOf(reqPara.getFeelStatus());
			Time totalTime = DateUtil.strToTime(reqPara.getTotalTime());
			Double calorie = Double.valueOf(reqPara.getCalorie());
		    Date runStartTime = DateUtil.strToDate(reqPara.getRunStartTime());
		    Date runEndTime = DateUtil.strToDate(reqPara.getRunEndTime());
		    String trajectory=reqPara.getTrajectory();
		    
		    FreeRun freeRun=new FreeRun();
		    freeRun.setCreateUser(userid);
		    freeRun.setIsDelete(false);
		    freeRun.setCreateDate(startDate);
		    freeRun.setRoadLength(roadLength);
		    freeRun.setSpeed(speed);
		    freeRun.setFeelStatus(feelStatus);
		    freeRun.setTotalTime(totalTime);
		    freeRun.setCalorie(calorie);
		    freeRun.setRunStartTime(runStartTime);
		    freeRun.setRunEndTime(runEndTime);
		    freeRun.setTrajectory(trajectory);
			
		    freeRunService.insert(freeRun);
		    
			resPara.setCode(CommCode.M_Y000000);
			resPara.setMessage(CommCode.M_Y000001);
		  }
		} catch (Exception e) {
			resPara.setCode(CommCode.M_A000000);
			resPara.setMessage(CommCode.M_SYS0001);

			logger.error("doPost error: ", e);
		}
		
		try {
			response.setContentType("application/json;charset=utf-8");
			response.setHeader("pragma", "no-cache");
			response.setHeader("cache-control", "no-cache");

			Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").excludeFieldsWithoutExposeAnnotation().serializeNulls().create();
			String jsonResult = gson.toJson(resPara);
	//		logger.info("FreeRunServlet jsonResult:" + jsonResult);

			PrintWriter out = response.getWriter();
			out.print(jsonResult);
			out.flush();
			out.close();

//			logger.info("doPost end run(s): " + DateUtil.calLastedTime(startDate));
		} catch (Exception e) {
			resPara.setCode(CommCode.M_A000000);
			resPara.setMessage(CommCode.M_SYS0003);

			logger.error("doPost error: ", e);
		}
	}

	@Override
	public FreeRunReqPara getReqPara(HttpServletRequest request) {

		FreeRunReqPara reqPara = null;

		try {
			BaseRequest baseRequest = RequestUtils.getRequestPara(request, new FreeRunReqPara());

			reqPara = (FreeRunReqPara) baseRequest.clone();

			String userid = request.getParameter("userid");
			String roadLength =request.getParameter("roadLength");
			String speed =request.getParameter("speed");
			String feelStatus =request.getParameter("feelStatus");
			String totalTime =request.getParameter("totalTime");
			String calorie =request.getParameter("calorie");
			String runStartTime =request.getParameter("runStartTime");
			String runEndTime =request.getParameter("runEndTime");
			String trajectory =request.getParameter("trajectory");
			
//			logger.info("reqPara userid:" + userid);
//			logger.info("reqPara roadLength:" + roadLength);
//			logger.info("reqPara speed:" + speed);
//			logger.info("reqPara feelStatus:" + feelStatus);
//			logger.info("reqPara totalTime:" + totalTime);
//			logger.info("reqPara calorie:" + calorie);
//			logger.info("reqPara runStartTime:" + runStartTime);
//			logger.info("reqPara runEndTime:" + runEndTime);
//			logger.info("reqPara trajectory:" + trajectory);

	        reqPara.setUserid(userid);
	        reqPara.setRoadLength(roadLength);
	        reqPara.setSpeed(speed);
	        reqPara.setFeelStatus(feelStatus);
	        reqPara.setTotalTime(totalTime);
	        reqPara.setCalorie(calorie);
	        reqPara.setRunStartTime(runStartTime);
	        reqPara.setRunEndTime(runEndTime);
	        reqPara.setTrajectory(trajectory);

		} catch (Exception e) {
			logger.error("getReqPara error: ", e);
		}

		return reqPara;
	}

	@Override
	public boolean isCheckField(BaseRequest reqPara, BaseResponse resPara) {
		boolean flag = false;

		FreeRunReqPara cdReqPara = (FreeRunReqPara) reqPara;
		BaseResponse cdResPara = (BaseResponse) resPara;

	    if (StringUtils.isEmpty(cdReqPara.getUserid())) {
			cdResPara.setCode(CommCode.M_A000000);
			cdResPara.setMessage(CommCode.M_P000002);
		}else if (StringUtils.isEmpty(cdReqPara.getRoadLength())) {
			cdResPara.setCode(CommCode.M_A000000);
			cdResPara.setMessage("跑步总距离为空");
		}else if (StringUtils.isEmpty(cdReqPara.getSpeed())) {
			cdResPara.setCode(CommCode.M_A000000);
			cdResPara.setMessage("速度为空");
		}else if (StringUtils.isEmpty(cdReqPara.getTotalTime())) {
			cdResPara.setCode(CommCode.M_A000000);
			cdResPara.setMessage("消耗时间为空");
		}else if (StringUtils.isEmpty(cdReqPara.getSpeed())) {
			cdResPara.setCode(CommCode.M_A000000);
			cdResPara.setMessage("速度为空");
		}else if (StringUtils.isEmpty(cdReqPara.getCalorie())) {
			cdResPara.setCode(CommCode.M_A000000);
			cdResPara.setMessage("消耗卡路里为空");
		}else if (StringUtils.isEmpty(cdReqPara.getRunStartTime())) {
			cdResPara.setCode(CommCode.M_A000000);
			cdResPara.setMessage("开始时间为空");
		}else if (StringUtils.isEmpty(cdReqPara.getRunEndTime())) {
			cdResPara.setCode(CommCode.M_A000000);
			cdResPara.setMessage("结束时间为空");
		}else if (StringUtils.isEmpty(cdReqPara.getTrajectory())) {
			cdResPara.setCode(CommCode.M_A000000);
			cdResPara.setMessage("地图轨迹为空");
		}else {
			flag = true;
		}

		return  flag;
	}

}
